import preferences from '@ohos.data.preferences';
class PreferencesUtil {

  prefMap: Map<string,preferences.Preferences> = new Map()

 async loadPreference(context,name: string){
    // preferences.getPreferences(context,name).then(pref=> {
    //     this.prefMap.set(name,pref)
    //   console.log('testTag',`加载Preferences[${name}]成功`)
    // })
    //   .catch(reason => {
    //     console.log('testTag',`加载Preferences[${name}]失败`,JSON.stringify(reason))
    //   })

   try {
     // 加载preferences
     let pref = await preferences.getPreferences(context,name)
     this.prefMap.set(name,pref)
   }
   catch (err) {
     console.log('testTag',`加载Preferences[${name}]失败`,JSON.stringify(err))
   }



  }





  async putPreferenceValue(name: string,key: string,value: preferences.ValueType) {
    if(!this.prefMap.has(name)){
      console.log('testTag',`Preferences[${name}]尚未初始化`)
      return
    }

    try {
      let pref =  this.prefMap.get(name)
      //写入数据
      await pref.put(key,value)
      // 刷盘
      await pref.flush()
      console.log('testTag',`保存Preferences[${name}.${key} = ${value}]成功`)
    }
    catch (err) {
      console.log('testTag',`保存Preferences[${name}]失败`,JSON.stringify(err))
    }


  }


  async getPreferenceValue(name: string,key:string,defaultValue: preferences.ValueType) {
    if(!this.prefMap.has(name)){
      console.log('testTag',`Preferences[${name}]尚未初始化!`)
      return
    }
    try {
      let pref = this.prefMap.get(name)
      // 读数据
      let value = await pref.get(key,defaultValue)
      console.log('testTag',`读取Preferences[${name}.${key} = ${value}]成功`)
      return value
    }
    catch (err){
      console.log('testTag',`读取Preferences[${name}.${key}]失败`,JSON.stringify(err))
    }
  }


  async deletePreferenceValue(name:string,key: string) {
    if(!this.prefMap.has(name)){
      console.log('testTag',`Preferences[${name}]尚未初始化!`)
      return
    }
    try {
      let pref = this.prefMap.get(name)
      await pref.delete(key)
      console.log('testTag',`删除Preferences[${name}.${key}]成功`)
    }
    catch (err) {
      console.log('testTag',`删除Preferences[${name}.${key}]失败`,JSON.stringify(err))
    }

  }


}

const preferencesUtil = new PreferencesUtil()

export default  preferencesUtil